<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>括号匹配</title>
  </head>
  <body>
    <script>
      const n = 4
      let ans = 0
      let left = n
      let right = n
      let res = []
      function process(l, r, str='(') {
        if (l < 0 || r < 0 || l > r) {
          return 0;
        }
        if (l === 0 && r === 0) {
          res.push(str);
          return 1;
        }
        return process(l - 1, r, str+'(') + process(l, r - 1, str+')')
      }
      ans = process(left-1,right);
      console.log(ans);
      console.log(res);
    </script>
  </body>
</html>
